<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Menu</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
</head>
<body>

<h1>Menu</h1>

<p>Creates, deletes, modifies and displays menus and menu items. Changes the tray icon and its tooltip. Controls whether the main window of a <a href="../Scripts.htm#ahk2exe">compiled script</a> can be opened.</p>

<pre class="Syntax">Menu, MenuName, Cmd [, P3, P4, P5]</pre>
<h3>Parameters</h3>
<dl>

  <dt>MenuName</dt>
  <dd><p>It can be TRAY or the name of any custom menu. A custom menu is automatically created the first time its name is used with the <em>Add</em> command. For example: <code>Menu, MyMenu, Add, Item1</code>.</p>
      <p>Once created, a custom menu can be displayed with the <em>Show</em> command. It can also be attached as a submenu to one or more other menus via the <em>Add</em> command.</p></dd>

  <dt>Cmd, P3, P4, P5</dt>
  <dd><p>These 4 parameters are dependent on each other. See list below for the allowed combinations.</p></dd>

</dl>

<h2>Add or Change Items in a Menu</h2>
<p><strong>Add [, MenuItemName, Label-or-Submenu, Pn]</strong>: This is a multipurpose command that adds a menu item, updates one with a new submenu or label, or converts one from a normal item into a submenu (or vice versa). If <em>MenuItemName</em> does not yet exist, it will be added to the menu. Otherwise, <em>MenuItemName</em> is updated with the newly specified <em>Label-or-Submenu</em>.</p>
<p>To add a menu separator line, omit all three parameters.</p>
<p>The label subroutine is run as a new <a href="../misc/Threads.htm">thread</a> when the user selects the menu item (similar to <a href="Gosub.htm">Gosub</a> and <a href="../Hotkeys.htm">hotkey subroutines</a>). If <em>Label-or-Submenu</em> is omitted, <em>MenuItemName</em> will be used as both the label and the menu item's name.</p>
<p id="Functor"><span class="ver">[v1.1.20+]:</span> If it is not the name of an existing label, <em>Label-or-Submenu</em> can be the name of a function, or a single variable reference containing a <a href="../objects/Functor.htm">function object</a>. For example, <code>%funcobj%</code> or <code>% funcobj</code>. Other expressions which return objects are currently unsupported. The function can optionally define parameters as shown below:</p>
<pre class="Syntax"><i>FunctionName</i>(ItemName, ItemPos, MenuName)</pre>
<p>To have <em>MenuItemName</em> become a submenu -- which is a menu item that opens a new menu when selected -- specify for <em>Label-or-Submenu</em> a colon followed by the <em>MenuName</em> of an existing custom menu. For example:</p>
<pre>Menu, MySubmenu, add, Item1
Menu, tray, add, This Menu Item Is A Submenu, :MySubmenu</pre>
<p>The final parameter may contain the letter P followed by the menu's <a href="../misc/Threads.htm">thread priority</a>, e.g. <code>P1</code>. If this parameter is omitted when adding a menu item, the priority will be 0, which is the standard default. If omitted when updating a menu item, the item's priority will not be changed. To change an existing item's priority only, omit the <em>Label-or-Submenu</em> parameter. Use a decimal (not hexadecimal) number as the priority.</p>
<p><strong><a name="Delete"></a>Delete [, MenuItemName]</strong>: Deletes <em>MenuItemName</em> from the menu. Standard menu items such as Exit (see below) cannot be individually deleted. If the <em>default</em> menu item is deleted, the effect will be similar to having used the <em>NoDefault</em> option. If <em>MenuItemName</em> is omitted, the entire <em>MenuName</em> menu will be deleted as will any menu items in other menus that use <em>MenuName</em> as a submenu.</p>
<p><strong>DeleteAll</strong>: Deletes all custom menu items from the menu, leaving the menu empty unless it contains the <em>standard</em> items (see below). Unlike a menu entirely deleted by the <em>Delete</em> command (see above), an empty menu still exists and thus any other menus that use it as a submenu will retain those submenus.</p>
<p><strong>Rename, MenuItemName [, NewName]</strong>: Changes <em>MenuItemName</em> to <em>NewName</em> (if <em>NewName</em> is blank, <em>MenuItemName</em> will be converted into a separator line). <em>NewName</em> must not be the same as any existing custom menu item. The menu item's current target label or submenu is unchanged.</p>
<p><strong>Check, MenuItemName</strong>: Adds a visible checkmark in the menu next to <em>MenuItemName</em> (if there isn't one already).</p>
<p><strong>Uncheck, MenuItemName</strong>: Removes the checkmark (if there is one) from <em>MenuItemName</em>.</p>
<p><strong>ToggleCheck, MenuItemName</strong>: Adds a checkmark if there wasn't one; otherwise, removes it.</p>
<p><strong>Enable, MenuItemName</strong>: Allows the user to once again select <em>MenuItemName</em> if was previously disabled (grayed).</p>
<p><strong>Disable, MenuItemName</strong>: Changes <em>MenuItemName</em> to a gray color to indicate that the user cannot select it.</p>
<p><strong>ToggleEnable, MenuItemName</strong>: Disables <em>MenuItemName</em> if it was previously enabled; otherwise, enables it.</p>
<p><strong>Default [, MenuItemName]</strong>: Changes the menu's default item to be <em>MenuItemName</em> and makes that item's font bold (setting a default item in menus other than TRAY is currently purely cosmetic). When the user double-clicks the tray icon, its default menu item is launched. If there is no default, double-clicking has no effect. If <em>MenuItemName</em> is omitted, the effect is the same as having used <em>NoDefault</em> below.</p>
<p><strong>NoDefault</strong>: For the tray menu: Changes the menu back to having its standard default menu item, which is OPEN for non-compiled scripts and none for <a href="../Scripts.htm#ahk2exe">compiled scripts</a> (except when the <em>MainWindow</em> option is in effect). If the OPEN menu item does not exist due to a previous use of the NoStandard command below, there will be no default and thus double-clicking the tray icon will have no effect. For menus other than TRAY: Any existing default item is returned to a non-bold font.</p>
<p><strong>Standard</strong>: Inserts the standard menu items at the bottom of the menu (if they are not already present). This command can be used with the tray menu or any other menu.</p>
<p><strong>NoStandard</strong>: Removes all standard (non-custom) menu items from the tray menu (if they are present).</p>
<h2 id="MenuIcon">Set or Remove a Menu Item's Icon <span class="ver">[AHK_L 17+]</span></h2>
<p><strong>Icon, MenuItemName, FileName [, IconNumber, IconWidth]</strong>: Sets <em>MenuItemName</em>'s icon. <em>FileName</em> can either be an icon file or any image in a format supported by AutoHotkey. To use an icon group other than the first one in the file, specify its number for <em>IconNumber</em> (if omitted, it defaults to 1). If <em>IconNumber</em> is negative, its absolute value is assumed to be the resource ID of an icon within an executable file. Specify the desired width of the icon in <em>IconWidth</em>. If the icon group indicated by <em>IconNumber</em> contains multiple icon sizes, the closest match is used and the icon is scaled to the specified size. See the Examples section for usage examples.</p>
<p>Currently it is necessary to specify "actual size" when setting the icon to preserve transparency on Windows Vista and later. For example:</p>
<pre>Menu, MenuName, Icon, MenuItemName, Filename.png,, 0</pre>
<p>Known limitation: Icons on Gui menu bars are positioned incorrectly on Windows XP and older.</p>
<p><strong>NoIcon, MenuItemName</strong>: Removes <em>MenuItemName</em>'s icon, if any.
<h2>Change the Tray Icon or ToolTip <span class="ver">(<em>MenuName</em> must be TRAY)</span></h2>
<p><strong><a name="Icon"></a>Icon [, FileName, IconNumber, 1]</strong>: Changes the script's icon to one of the ones from <em>FileName</em>. The following types of files are supported: ICO, CUR, ANI, EXE, DLL, CPL, SCR, and other types that contain icon resources. To use an icon group other than the first one in the file, specify its number for <em>IconNumber</em> (if omitted, it defaults to 1). For example, <strong>2</strong> would load the default icon from the second icon group. If <em>IconNumber</em> is negative, its absolute value is assumed to be the resource ID of an icon within an executable file. Specify an asterisk (*) for <em>FileName</em> to restore the script to its default icon.</p>
<p>The last parameter: Specify 1 for the last parameter to freeze the icon, or 0 to unfreeze it (or leave it blank to keep the frozen/unfrozen state unchanged). When the icon has been frozen, <a href="Pause.htm">Pause</a> and <a href="Suspend.htm">Suspend</a> will not change it. Note: To freeze or unfreeze the <em>current</em> icon, use 1 or 0 as in the following example: <code>Menu, Tray, Icon,,, 1</code>.</p>
<p>Changing the tray icon also changes the icon displayed by <a href="InputBox.htm">InputBox</a>, <a href="Progress.htm">Progress</a>, and subsequently-created <a href="Gui.htm">GUI</a> windows. <a href="../Scripts.htm#ahk2exe">Compiled scripts</a> are also affected even if a custom icon was specified at the time of compiling. Note: Changing the icon will not unhide the tray icon if it was previously hidden by means such as <a href="_NoTrayIcon.htm">#NoTrayIcon</a>; to do that, use <code>Menu, Tray, Icon</code> (with no parameters).</p>
<p><a name="distort"></a>Slight distortion may occur when loading tray icons from file types other than .ICO. This is especially true for 16x16 icons. To prevent this, store the desired tray icon inside a .ICO file.</p>
<p>There are some icons built into the operating system's DLLs and CPLs that might be useful. For example: <code>Menu, Tray, Icon, Shell32.dll, 174</code>.</p>
<p>The built-in variables <strong>A_IconNumber</strong> and <strong>A_IconFile</strong> contain the number and name (with full path) of the current icon (both are blank if the icon is the default).</p>
<p><strong>Icon</strong> (with no parameters): Creates the tray icon if it isn't already present. This will override <a href="_NoTrayIcon.htm">#NoTrayIcon</a> if that directive is also present in the script.</p>
<p><strong>NoIcon</strong>: Removes the tray icon if it exists. If this command is used at the very top of the script, the tray icon might be briefly visible when the script is launched. To prevent that, use <a href="_NoTrayIcon.htm">#NoTrayIcon</a> instead. The built-in variable <strong>A_IconHidden</strong> contains 1 if the tray icon is currently hidden or 0 otherwise.</p>
<p><strong>Tip [, Text]</strong>: Changes the tray icon's tooltip -- which is displayed when the mouse hovers over it. To create a multi-line tooltip, use the linefeed character (`n) in between each line, e.g. Line1`nLine2. Only the first 127 characters of <em>Text</em> are displayed, and <em>Text</em> is truncated at the first tab character, if present. If <em>Text</em> is omitted, the tooltip is restored to its default text. The built-in variable <strong>A_IconTip</strong> contains the current text of the tooltip (blank if the text is at its default).</p>
<h2>Miscellaneous Commands</h2>
<p><strong><a name="Show"></a>Show [, X, Y]</strong>: Displays <em>MenuName</em>, allowing the user to select an item with arrow keys, menu shortcuts (underlined letters), or the mouse. Any menu can be shown, including the tray menu but with the exception of <a href="Gui.htm">GUI</a> menu bars. If both X and Y are omitted, the menu is displayed at the current position of the mouse cursor. If only one of them is omitted, the mouse cursor's position will be used for it. X and Y are relative to the active window. Specify &quot;<a href="CoordMode.htm">CoordMode</a>, Menu&quot; beforehand to make them relative to the entire screen.</p>
<p><strong>Color, ColorValue [, Single]</strong>: Changes the background color of the menu to <em>ColorValue</em>, which is one of the 16 primary HTML color names or a 6-digit RGB color value (see <a href="Progress.htm#colors">color chart</a>). Leave <em>ColorValue</em> blank (or specify the word Default) to restore the menu to its default color. If the word Single is not present as the next parameter, any submenus attached to this menu will also be changed in color.</p>
<p><strong>Click, ClickCount</strong>: Specify 1 for <em>ClickCount</em> to allow a single-click to activate the tray menu's default menu item. Specify 2 for <em>ClickCount</em> to return to the default behavior (double-click). For example: <code>Menu, Tray, Click, 1</code>.</p>
<p><strong><a name="MainWindow"></a>MainWindow</strong>: This command affects <a href="../Scripts.htm#ahk2exe">compiled scripts</a> only. It allows the script's main window to be opened via the tray icon, which is otherwise impossible. It also enables the items in the main window's View menu such as &quot;Lines most recently executed&quot;, which allows viewing of the script's source code and other info. <em>MenuName</em> must be TRAY.</p>
<p><strong>NoMainWindow</strong> (default): This command affects <a href="../Scripts.htm#ahk2exe">compiled scripts</a> only. It restores the script to its default behavior, that is, it prevents the main window from being opened. Even while this option is in effect, the following commands are still able to show the main window when they are encountered in the script at runtime: <a href="ListLines.htm">ListLines</a>, <a href="ListVars.htm">ListVars</a>, <a href="ListHotkeys.htm">ListHotkeys</a>, and <a href="KeyHistory.htm">KeyHistory</a>. <em>MenuName</em> must be TRAY.</p>
<p><strong>UseErrorLevel [, off]</strong>: If this option is never used in the script, it defaults to OFF. The OFF setting displays a dialog and terminates the <a href="../misc/Threads.htm">current thread</a> whenever the Menu command generates an error. Specify <code>Menu, Tray, UseErrorLevel</code> to prevent the dialog and thread termination; instead, <a href="../misc/ErrorLevel.htm">ErrorLevel</a> will be set to 1 if there was a problem or 0 otherwise. To turn this option back off, specify OFF for the next parameter. This setting is global, meaning it affects all menus, not just <em>MenuName</em>.</p>
<h2>Remarks</h2>
<p>To underline one of the letters in <em>a</em> menu item's name, precede that letter with an ampersand (&amp;). When the menu is displayed, such an item can be selected by pressing the corresponding key on the keyboard. To display a literal ampersand, specify two consecutive ampersands as in this example: &quot;Save &amp;&amp; Exit&quot;</p>
<p>The names of menus and menu items can be up to 260 characters long.</p>
<p>When referring to an existing menu or menu item, the name is not case sensitive but any ampersands must be included. For example: &amp;Open</p>
<p>Separator lines can be added to the menu by using <code>Menu, Tray, Add</code> (i.e. omit all other parameters). However, separator lines currently cannot be individually deleted. To work around this, use <code>Menu, Tray, DeleteAll</code> and then re-add your custom menu items.</p>
<p>New menu items are always added at the bottom of the menu. For the tray menu: To put your menu items on top of the standard menu items (after adding your own menu items) run <code>Menu, Tray, NoStandard</code> followed by <code>Menu, Tray, Standard</code>.</p>
<p>The standard menu items such as &quot;Pause Script&quot; and &quot;Suspend Hotkeys&quot; cannot be individually operated upon by any menu sub-command.</p>
<p>If a menu ever becomes completely empty -- such as by using <code>Menu, MyMenu, DeleteAll</code> -- it cannot be shown. If the tray menu becomes empty, right-clicking and double-clicking the tray icon will have no effect (in such cases it is usually better to use <a href="_NoTrayIcon.htm">#NoTrayIcon</a>).</p>
<p>If a menu item's subroutine is already running and the user selects the same menu item again, a new <a href="../misc/Threads.htm">thread</a> will be created to run that same subroutine, interrupting the previous thread. To instead buffer such events until later, use <a href="Critical.htm">Critical</a> as the subroutine's first line  (however, this will also buffer/defer other threads such as the press of a hotkey).</p>
<p>Whenever a  subroutine is launched via a menu item, it starts off fresh with the default values for settings such as <a href="SendMode.htm">SendMode</a>. These defaults can be changed in the <a href="../Scripts.htm#auto">auto-execute section</a>.</p>
<p>The built-in variables <strong><a href="../Variables.htm#ThisMenuItem">A_ThisMenuItem</a></strong> and <strong><a href="../Variables.htm#ThisMenuItemPos">A_ThisMenuItemPos</a></strong> contain the name and position of the custom menu item most recently selected by the user (blank if none). Similarly, <strong>A_ThisMenu</strong> is the name of the menu from which <strong>A_ThisMenuItem</strong> was selected. These variables are useful when building a menu whose contents are not always the same. In such a case, it is usually best to point all such menu items to the same label and have that label refer to the above variables to determine what action to take.</p>
<p>To keep a non-hotkey, non-<a href="Gui.htm">GUI</a> script running -- such as one that contains only custom menus or menu items -- use <a href="_Persistent.htm">#Persistent</a>.</p>
<h2>Related</h2>
<p><a href="Gui.htm">GUI</a>, <a href="../misc/Threads.htm">Threads</a>, <a href="Thread.htm">Thread</a>, <a href="Critical.htm">Critical</a>, <a href="_NoTrayIcon.htm">#NoTrayIcon</a>, <a href="Gosub.htm">Gosub</a>, <a href="Return.htm">Return</a>, <a href="SetTimer.htm">SetTimer</a>, <a href="_Persistent.htm">#Persistent</a></p>
<h2>Examples</h2>
<pre class="NoIndent"><em>; EXAMPLE #1: This is a working script that adds a new menu item to the bottom of the tray icon menu.</em>

#Persistent  <em>; Keep the script running until the user exits it.</em>
Menu, tray, add  <em>; Creates a separator line.</em>
Menu, tray, add, Item1, MenuHandler  <em>; Creates a new menu item.</em>
return

MenuHandler:
MsgBox You selected %A_ThisMenuItem% from menu %A_ThisMenu%.
return</pre>
<p>&nbsp;</p>
<pre class="NoIndent"><em>; EXAMPLE #2: This is a working script that creates a popup menu that is displayed when the user presses the Win-Z hotkey.</em>

<em>; Create the popup menu by adding some items to it.</em>
Menu, MyMenu, Add, Item1, MenuHandler
Menu, MyMenu, Add, Item2, MenuHandler
Menu, MyMenu, Add  <em>; Add a separator line.</em>

<em>; Create another menu destined to become a submenu of the above menu.</em>
Menu, Submenu1, Add, Item1, MenuHandler
Menu, Submenu1, Add, Item2, MenuHandler

<em>; Create a submenu in the first menu (a right-arrow indicator). When the user selects it, the second menu is displayed.</em>
Menu, MyMenu, Add, My Submenu, :Submenu1

Menu, MyMenu, Add  <em>; Add a separator line below the submenu.</em>
Menu, MyMenu, Add, Item3, MenuHandler  <em>; Add another menu item beneath the submenu.</em>
return  <em>; End of script's auto-execute section.</em>

MenuHandler:
MsgBox You selected %A_ThisMenuItem% from the menu %A_ThisMenu%.
return

#z::Menu, MyMenu, Show  <em>; i.e. press the Win-Z hotkey to show the menu.</em></pre>
<p>&nbsp;</p>
<pre class="NoIndent"><em>; EXAMPLE #3: This is a working script that demonstrates some of the various menu commands.</em>

#Persistent
#SingleInstance
menu, tray, add <em>; separator</em>
menu, tray, add, TestToggle&amp;Check
menu, tray, add, TestToggleEnable
menu, tray, add, TestDefault
menu, tray, add, TestStandard
menu, tray, add, TestDelete
menu, tray, add, TestDeleteAll
menu, tray, add, TestRename
menu, tray, add, Test
return

<em>;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;</em>

TestToggle&amp;Check:
menu, tray, ToggleCheck, TestToggle&amp;Check
menu, tray, Enable, TestToggleEnable <em>; Also enables the next test since it can't undo the disabling of itself.</em>
menu, tray, add, TestDelete <em>; Similar to above.</em>
return

TestToggleEnable:
menu, tray, ToggleEnable, TestToggleEnable
return

TestDefault:
if default = TestDefault
{
    menu, tray, NoDefault
    default =
}
else
{
    menu, tray, Default, TestDefault
    default = TestDefault
}
return

TestStandard:
if standard &lt;&gt; n
{
    menu, tray, NoStandard
    standard = n
}
else
{
    menu, tray, Standard
    standard = y
}
return

TestDelete:
menu, tray, delete, TestDelete
return

TestDeleteAll:
menu, tray, DeleteAll
return

TestRename:
if NewName &lt;&gt; renamed
{
    OldName = TestRename
    NewName = renamed
}
else
{
    OldName = renamed
    NewName = TestRename
}
menu, tray, rename, %OldName%, %NewName%
return

Test:
MsgBox, You selected &quot;%A_ThisMenuItem%&quot; in menu &quot;%A_ThisMenu%&quot;.
return</pre>
<pre class="NoIndent"><em>; EXAMPLE #4: This is a working script that adds icons to its menu items.</em>
Menu, FileMenu, Add, Script Icon, MenuHandler
Menu, FileMenu, Add, Suspend Icon, MenuHandler
Menu, FileMenu, Add, Pause Icon, MenuHandler
Menu, FileMenu, Icon, Script Icon, %A_AhkPath%, 2 <em>;Use the 2nd icon group from the file</em>
Menu, FileMenu, Icon, Suspend Icon, %A_AhkPath%, -206 <em>;Use icon with resource identifier 206</em>
Menu, FileMenu, Icon, Pause Icon, %A_AhkPath%, -207 <em>;Use icon with resource identifier 207</em>
Menu, MyMenuBar, Add, &amp;File, :FileMenu
Gui, Menu, MyMenuBar
Gui, Add, Button, gExit, Exit This Example
Gui, Show
MenuHandler:
Return

Exit:
ExitApp
</pre>
</body>
</html>
